package com.jqkb.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.fasterxml.jackson.databind.JsonNode;
import com.jqkb.R;
import com.jqkb.pages.ForwardDetailActivity;
import com.jqkb.service.bean.ForwardBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by alimjantursun on 18/5/29.
 */
public class ActionForwardAdapter extends RecyclerView.Adapter {


    private final Context context;
    private final List<ForwardBean> beanList;
    private List<ForwardBean> checkedLists = new ArrayList<>();
    private ForwardDetailActivity forwardDetailActivity;
    private JsonNode current;

    public ActionForwardAdapter(Context context, List<ForwardBean> beanList) {
        this.context = context;
        this.beanList = beanList;
    }

    public List<ForwardBean> getCheckList() {
        checkedLists.clear();
        for (ForwardBean bean : beanList) {
            if (bean.isCheck()) {
                checkedLists.add(bean);
            }
        }
        return checkedLists;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.info_list_item, parent, false);
        return new ClassHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ForwardBean bean = beanList.get(position);
        ((ClassHolder) holder).itemView.setTag(bean);
        ((ClassHolder) holder).shangbaorenTv.setText(bean.getSendUserName());
        ((ClassHolder) holder).shijianTv.setText(bean.getCreateDate());
        ((ClassHolder) holder).mubiaoTv.setText(bean.getTargetUserName());
        ((ClassHolder) holder).checkBox.setChecked(bean.isCheck());
        if (bean.isVisible()) {
            ((ClassHolder) holder).checkBox.setVisibility(View.VISIBLE);
        } else {
            ((ClassHolder) holder).checkBox.setVisibility(View.GONE);
        }

        ((ClassHolder) holder).checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                bean.setCheck(isChecked);
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long actionForwardId = bean.getId();
                forwardDetailActivity.loadData(null, actionForwardId);
            }
        });

        if (current.path("id").asLong()==bean.getId()){
            holder.itemView.setBackgroundColor(0xffb4ccf8);
        }

    }

    public void setActivity(ForwardDetailActivity forwardDetailActivity) {
        this.forwardDetailActivity = forwardDetailActivity;
    }

    public void setCurrent(JsonNode current) {
        this.current = current;
    }

    public JsonNode getCurrent() {
        return current;
    }

    public class ClassHolder extends RecyclerView.ViewHolder {


        TextView shangbaorenTv;
        TextView shijianTv;
        TextView mubiaoTv;
        CheckBox checkBox;

        public ClassHolder(View itemView) {
            super(itemView);
            shangbaorenTv = (TextView) itemView.findViewById(R.id.shangbaorenTv);
            shijianTv = (TextView) itemView.findViewById(R.id.shijianTv);
            mubiaoTv = (TextView) itemView.findViewById(R.id.mubiaoTv);
            checkBox = (CheckBox) itemView.findViewById(R.id.checkBox2);


        }
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }


}
